package com.emmy.walnut.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.emmy.walnut.R;
import com.emmy.walnut.model.Record;
import com.emmy.walnut.model.RecordByDate;
import com.emmy.walnut.util.BusinessUtil;
import com.emmy.walnut.util.TimeStampUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ItemDetailAdapter
 * @Description TODO
 * @Author zhouxuan
 * @Date 2020/02/11 11:01
 * @Version 1.0
 */
public class ItemDetailAdapter extends RecyclerView.Adapter{

    private Context mContext;
    private List mList = new ArrayList();
    private List recordWithDates = new ArrayList();

    public static int ITEM_TYPE_DATE = 1;
    public static int ITEM_TYPE_DATA = 0;

    public ItemDetailAdapter(Context context, List<Record> list){
        mContext = context;
        if (list!=null) {
            mList = list;
            recordWithDates = BusinessUtil.getRecordWithDate(mList);
        }
    }

    public List getmList() {
        return mList;
    }

    public void setmList(List mList) {
        this.mList = mList;
        this.recordWithDates = BusinessUtil.getRecordWithDate(mList);
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ITEM_TYPE_DATA){
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_detail_data,
                    parent, false);
            return new DataViewHolder(view);
        }else{
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_detail_date,
                    parent, false);
            return new DateViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Object object = recordWithDates.get(position);
        if (object instanceof Record){
            DataViewHolder dataViewHolder = (DataViewHolder)holder;
            Record record = (Record)object;
            dataViewHolder.activity_text_view.setText(record.getLabelName());
            dataViewHolder.duration_text_view.setText(
                    TimeStampUtil.getFormatTime((int)(record.getEndTimestamp() - record.getStartTimestamp())));
        }else{
            DateViewHolder dateViewHolder = (DateViewHolder)holder;
            Long timestamp = (Long)object;
            dateViewHolder.dateView.setText(TimeStampUtil.getDayFormat(timestamp));
        }
    }

    @Override
    public int getItemViewType(int position) {
        Object object = recordWithDates.get(position);
        if (object instanceof Record){
            return ITEM_TYPE_DATA;
        }
        return ITEM_TYPE_DATE;
    }

    @Override
    public int getItemCount() {
        return recordWithDates.size();
    }

    class DateViewHolder extends RecyclerView.ViewHolder{

        TextView dateView;

        DateViewHolder(View view){
            super(view);
            dateView = view.findViewById(R.id.date);
        }
    }

    class DataViewHolder extends RecyclerView.ViewHolder{

        TextView activity_text_view;
        TextView duration_text_view;

        DataViewHolder(View view){
            super(view);
            activity_text_view = view.findViewById(R.id.activity_item_name);
            duration_text_view = view.findViewById(R.id.duration);
        }
    }

}
